<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class CoreViewedHistoryBase extends Base {
	
	//constructor
	function CoreViewedHistoryBase($owner = 0) {
		parent::Base();
	}
	
	//methods
	function init() {
		$this->_ip_address = util_get_ip_address();
		$this->_session_id = $this->_get_session_id();
		$this->_users_id = $_SESSION['suser']->userid;
		$this->_users_name =$_SESSION['suser']->username;
		
		$this->_current_time = time();
		$this->_xx_mins_ago = ($current_time - 3600 * 24);
		
		$this->_time_limit = true;
	}
	
	function add($value, $type) {
		if( $this->_check_exist() ) {
			$this->_insert_row($type . '_' . $value);
		} else {
			$this->_update_row('|' . $type . '_' . $value);
		}
	}
	
	function clear() {
		
	}
	
	function get($type = '') {
		return $this->_get_list($type);
	}
	
	//
	function _not_null($value) {
		return util_not_null($value);
	}
	
	function _check_exist() {
		if( $this->_time_limit == true ) {
			$query = "select id from pp_viewed_history where session_id = '" . (int)$this->_session_id . "' and ip_address = '" . $this->_ip_address . "' and time_last_click < '" . $this->__xx_mins_ago . "' order by id desc limit 1";
		} else {
			$query = "select id as count from pp_viewed_history where session_id = '" . (int)$this->_session_id . "' and ip_address = '" . $this->_ip_address . "' order by id desc limit 1";
		}
		$result = $this->adb->getOne($query);
		
		if( $result['id'] > 0 ) {
			$this->_record_id = $result['id'];
			return true;
		}
		
		return false;
	}
	
	function _get_row() {
		if( $this->_time_limit == true ) {
			$query = "select * from pp_viewed_history where session_id = '" . (int)$this->_session_id . "' and ip_address = '" . $this->_ip_address . "' and time_last_click < '" . $this->__xx_mins_ago . "' order by id desc limit 1";
		} else {
			$query = "select * as count from pp_viewed_history where session_id = '" . (int)$this->_session_id . "' and ip_address = '" . $this->_ip_address . "' order by id desc limit 1";
		}
		$row = $this->adb->getOne($query);
		
		return $row;
	}
	
	function _insert_row($value) {
		$query = "insert into pp_viewed_history (users_id, users_name, session_id, ip_address, time_entry, time_last_click, viewed_value) where ('" . $this->_users_id . "', '" . $this->_users_name . "', '" . $this->_session_id . "', '" . $this->_ip_address . "', '" . $this->_current_time . "', '', '" . $value . "')";
		$this->adb->query($query);
		
		return true;
	}
	
	function _update_row($value) {
		if( $this->_time_limit == true ) {
			$query = "update pp_viewed_history set users_id = '" . (int)$this->_users_id . "', users_name = '" . $this->_users_name . "', time_last_click = now(), viewed_value = viewed_value + '" . $value . "') where id = '" . $this->_record_id . "'";
		} else {
			$query = "update pp_viewed_history set users_id = '" . (int)$this->_users_id . "', users_name = '" . $this->_users_name . "', time_last_click = now(), viewed_value = viewed_value + '" . $value . "') where id = '" . $this->_record_id . "'";
		}
		$this->adb->query($query);
		
		return true;
	}
	
	function _get_list($type = '') {
		$row = $this->_get_row();
		
		$list_array = array();
		if( $this->_not_null($row['viewed_history']) ) {
			$tmparray = explode('|', $row['viewed_value']);
			if( is_array($tmparray) ) {
				for($i=0, $n=sizeof($tmparray); $i<$n; $i++) {
					$tmp = explode('_', $tmparray[$i]);
					if( sizeof($tmp) == 2 ) {
						if( $type == '' ) {
							$list_array[] = $tmp;
						} elseif( $tmp['0'] == $type ) {
							$list_array[] = $tmp;
						}
					}
				}
			}
		}
		
		return $list_array;
	}
}
//
?>